﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.Controls
{
    public partial class RoleListCtrl :BaseListControl, IListControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            Object o = grid;
        }

        public POSSystem.BLL.Roles RoleList
        {
            get { return (POSSystem.BLL.Roles)SourceObject; }
            set { SourceObject = value; }
        }

        public void Update()
        {
            for (int i = 0; i < grid.Items.Count; i++)
            {
                DataGridItem item = grid.Items[i];
                Control ctrl = item.FindControl("cbSelect");
                if (ctrl != null && ctrl is CheckBox && ((CheckBox)ctrl).Checked && ((CheckBox)ctrl).Attributes["ItemID"] != null)
                {
                    string oldFilter = RoleList.Filter;
                    CheckBox chkSales = item.FindControl("chkSales") as CheckBox;
                    CheckBox chkPurchase = item.FindControl("chkPurchase") as CheckBox;
                    CheckBox chkProducts = item.FindControl("chkProducts") as CheckBox;
                    CheckBox chkCustomers = item.FindControl("chkCustomers") as CheckBox;
                    CheckBox chkSuppliers = item.FindControl("chkSuppliers") as CheckBox;
                    CheckBox chkRepair = item.FindControl("chkRepair") as CheckBox;
                    CheckBox chkAdmin = item.FindControl("chkAdmin") as CheckBox;
                    CheckBox chkReport = item.FindControl("chkReport") as CheckBox;
                    CheckBox chkSysAdmin = item.FindControl("chkSysAdmin") as CheckBox;
                    CheckBox chkContracts = item.FindControl("chkContracts") as CheckBox;
                    CheckBox chkEditSales = item.FindControl("chkEditSales") as CheckBox;
                    CheckBox chkExpense = item.FindControl("chkExpense") as CheckBox;

                    RoleList.Filter = "ID = " + ((CheckBox)ctrl).Attributes["ItemID"].ToString();

                    RoleList.Sales = chkSales.Checked;
                    RoleList.Purchase = chkPurchase.Checked;
                    RoleList.Products = chkProducts.Checked;
                    RoleList.Report = chkReport.Checked;
                    RoleList.Repair = chkRepair.Checked;
                    RoleList.Customers = chkCustomers.Checked;
                    RoleList.Suppliers = chkSuppliers.Checked;
                    RoleList.Admin = chkAdmin.Checked;
                    RoleList.SystemAdmin = chkSysAdmin.Checked;
                    RoleList.Contracts = chkContracts.Checked;
                    RoleList.EditSales = chkEditSales.Checked;
                    RoleList.Expense = chkExpense.Checked;
                    RoleList.Filter = oldFilter;
                }

                RoleList.MoveNext();
            }
            RoleList.Save();
        }

        #region IListControl Members

        public void DeleteCheckedItems()
        {
            try
            {
                String checkedItemID = "";
                for (int i = 0; i < grid.Items.Count; i++)
                {
                    DataGridItem item = grid.Items[i];
                    Control ctrl = item.FindControl("cbSelect");
                    if (ctrl != null && ctrl is CheckBox && ((CheckBox)ctrl).Checked && ((CheckBox)ctrl).Attributes["ItemID"] != null)
                        checkedItemID += ((CheckBox)ctrl).Attributes["ItemID"].ToString() + ",";
                }
                if (checkedItemID != "")
                    checkedItemID = checkedItemID.Remove(checkedItemID.Length - 1);
                checkedItemID = "(" + checkedItemID + ")";

                String oldFilter = Filter;
                if (!String.IsNullOrEmpty(oldFilter))
                    Filter = Filter + " and (" + POSSystem.BLL.Roles.ColumnNames.ID + " in " + checkedItemID + ")";
                else
                    Filter = POSSystem.BLL.Roles.ColumnNames.ID + " in " + checkedItemID;

                RoleList.Rewind();
                for (int i = 0; i < RoleList.RowCount; i++)
                {
                    if (POSSystem.BLL.Roles.IsRoleInUse(RoleList.ID))
                        throw new Exception(string.Format("Role {0} has been used.", RoleList.RoleName));
                    RoleList.MoveNext();
                }

                RoleList.Rewind();
                for (int i = 0; i < RoleList.RowCount; i++)
                {
                    if(POSSystem.BLL.Roles.IsRoleInUse(RoleList.ID))
                        throw new Exception (string.Format("Role {0} has been used."));
                    RoleList.Deleted = true;
                    RoleList.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                    RoleList.LastModifiedByUser = SessionUser.ID;
                    RoleList.MoveNext();
                }

                if (!SaveOnCommit)
                    RoleList.Save();

                Filter = oldFilter;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteSelectedItem()
        {
            throw new NotImplementedException();
        }

        public void AddNewItem(MyGeneration.dOOdads.SqlClientEntity newItem)
        {
            throw new NotImplementedException();
        }

        public void UpdateItem(MyGeneration.dOOdads.SqlClientEntity updatedItem)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}